<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">招聘管理</el-breadcrumb-item>
      <el-breadcrumb-item>招聘渠道</el-breadcrumb-item>
    </el-breadcrumb>
    <br>
    <br>

    <div>
      <!-- 搜索筛选 -->
      <el-form :inline="true"
               :model="formInline"
               class="user-search">
        <el-form-item label="搜索：">
          <el-input size="small"
                    v-model="formInline.name"
                    placeholder="渠道名称"></el-input>
        </el-form-item>

        <el-form-item prop="type">
          <select class="se_input"
                  v-model="formInline.lin_type">
            <option selected
                    value="渠道类型">渠道类型</option>
            <option value="招聘网站">招聘网站</option>
            <option value="招聘会">招聘会</option>
            <option value="猎头公司">猎头公司</option>
            <option value="其他">其他</option>
          </select>
        </el-form-item>

        <el-form-item prop="status">
          <select class="se_input"
                  id="status_input"
                  v-model="formInline.lin_status">
            <option selected
                    value="渠道状态">渠道状态</option>
            <option value="启用">启用</option>
            <option value="禁用">禁用</option>
          </select>
        </el-form-item>

        <el-form-item style="position: relative;left: 300px;">
          <el-button size="small"
                     type="primary"
                     icon="el-icon-search"
                     @click="search">搜索</el-button>
          <el-button size="small"
                     type="primary"
                     icon="el-icon-edit"
                     @click="flush">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 分割线 -->
      <el-divider></el-divider>
      <el-form>
        <el-form-item>
          <el-button size="small"
                     type="primary"
                     icon="el-icon-plus"
                     @click="handleEdit()">创建</el-button>
          <el-button size="small"
                     type="primary"
                     icon="el-icon-upload el-icon--right"
                     @click="exportExcelSelect()">导出</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div style="margin-top: 10px">
      <!--列表-->
      <el-table size="small"
                :data="listData"
                highlight-current-row
                v-loading="loading"
                border
                element-loading-text="拼命加载中"
                style="width: 100%;"
                @selection-change="handleSelectionChange">
        <el-table-column align="center"
                         type="selection"
                         width="60">
        </el-table-column>
        <el-table-column sortable
                         prop="id"
                         label="序号"
                         width="80">
        </el-table-column>
        <el-table-column sortable
                         prop="num"
                         label="渠道编码"
                         width="100">
        </el-table-column>
        <el-table-column sortable
                         prop="name"
                         label="渠道名称"
                         width="150">
        </el-table-column>
        <el-table-column sortable
                         prop="type"
                         label="渠道类型"
                         width="100">
        </el-table-column>
        <el-table-column sortable
                         prop="status"
                         label="渠道状态"
                         width="100">
        </el-table-column>
        <el-table-column align="center"
                         label="操作"
                         min-width="200">
          <template slot-scope="scope">
            <el-button size="mini"
                       @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini"
                       type="danger"
                       @click="deleteUser(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <Pagination v-bind:child-msg="pageparm"
                  @callFather="callFather"
                  style="position: relative;top: 1px;"></Pagination>
    </div>

    <!-- 编辑界面 -->
    <el-dialog :title="title"
               :visible.sync="editFormVisible"
               width="30%"
               @click="closeDialog">
      <el-form label-width="120px"
               :model="editForm"
               :rules="rules"
               ref="editForm">
        <el-form-item label="渠道ID"
                      prop="id">
          <el-input size="small"
                    v-model="editForm.id"
                    auto-complete="off"
                    :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="编码名称"
                      prop="name">
          <el-input size="small"
                    v-model="editForm.name"
                    auto-complete="off"
                    placeholder="编码名称"></el-input>
        </el-form-item>

        <el-form-item label="渠道状态"
                      prop="status">
          <select v-model="editForm.status"
                  class="se_input"
                  style="width: 300px;">
            <option selected
                    value="渠道状态">渠道状态</option>
            <option value="启用">启用</option>
            <option value="禁用">禁用</option>
          </select>
        </el-form-item>
        <el-form-item label="渠道描述"
                      prop="description">
          <el-input size="small"
                    v-model="editForm.description"
                    auto-complete="off"
                    placeholder="渠道描述"></el-input>
        </el-form-item>

        <el-form-item label="渠道编码"
                      prop="num">
          <el-input size="small"
                    v-model="editForm.num"
                    auto-complete="off"
                    placeholder="渠道编码"></el-input>
        </el-form-item>

        <el-form-item label="渠道类型"
                      prop="type">
          <select name=""
                  class="se_input"
                  style="width: 300px;"
                  v-model="editForm.type">
            <option selected
                    value="渠道类型">渠道类型</option>
            <option value="招聘网站">招聘网站</option>
            <option value="招聘会">招聘会</option>
            <option value="猎头公司">猎头公司</option>
            <option value="其他">其他</option>
          </select>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button size="small"
                   @click="closeDialog">取消</el-button>
        <el-button size="small"
                   type="primary"
                   :loading="loading"
                   class="title"
                   @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>

    <!-- 预览弹窗表格 -->
    <el-dialog title="表格保存预览"
               width="40%"
               :visible.sync="selectWindow">
      <el-table :data="selectData"
                id="selectTable"
                height="380px">
        <el-table-column sortable
                         prop="id"
                         label="序号"
                         width="80">
        </el-table-column>
        <el-table-column sortable
                         prop="num"
                         label="渠道编码"
                         width="100">
        </el-table-column>
        <el-table-column sortable
                         prop="name"
                         label="渠道名称"
                         width="150">
        </el-table-column>
        <el-table-column sortable
                         prop="type"
                         label="渠道类型"
                         width="100">
        </el-table-column>
        <el-table-column sortable
                         prop="status"
                         label="渠道状态"
                         width="100">
        </el-table-column>

      </el-table>

      <div slot="footer"
           class="dialog-footer">
        <el-button type="primary"
                   @click="exportExcel">确定保存</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from '../../components/Pagination'
import htmlToExcel from '@/utils/htmlToExcel'
export default {
  // name: "ExcelPage",
  data() {
    return {
      //表格中选中的数据
      selectData: [],
      selectWindow: false,
      value: '',
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        id: '',
        num: '',
        name: '',
        type: '',
        status: '',
        description: '',
        addOr: '',
        establish: '',
        establishTime: '',
        update: '',
        updateTime: '',
      },
      //   rules表单验证
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
        ],
        deptNo: [
          { required: true, message: '请输入部门代码', trigger: 'blur' },
        ],
      },
      formInline: {
        page: 1,
        limit: 10,
        name: '',
        type: '',
        status: '',
        lin_status: '渠道状态',
        lin_type: '渠道类型',
      },
      // 逻辑删除招聘渠道
      seletedata: {
        ids: '',
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
    }
  },
  // 注册组件
  components: {
    Pagination,
  },

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },
  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取公司列表
    getdata(parameter) {
      this.loading = true
      // 模拟数据开始
      this.loading = false
      this.pageparm.currentPage = this.formInline.page
      this.pageparm.pageSize = this.formInline.limit
      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      if (this.formInline.lin_status == '启用') {
        this.formInline.status = 1
      }
      if (this.formInline.lin_status == '禁用') {
        this.formInline.status = 2
      }
      if (this.formInline.status == '渠道状态') {
        this.formInline.status = 0
      }
      if (this.formInline.type == '渠道类型') {
        this.formInline.type = ''
      }
      if (this.formInline.lin_type != '渠道类型') {
        this.formInline.type = this.formInline.lin_type
      }
      this.$axios
        .post(
          '/recruitment/find/' +
            this.pageparm.currentPage +
            '/' +
            this.pageparm.pageSize,
          this.formInline
        )
        .then((res) => {
          console.log(res)
          this.loading = false
          if (res.code == 200) {
            this.listData = res.obj.records
            /***
             * 遍历循环listData 修改状态值
             */
            this.listData.forEach((item) => {
              if (item.status == 1) {
                item.status = '启用'
              } else {
                item.status = '禁用'
              }
            })
            this.pageparm.currentPage = res.obj.current
            this.pageparm.pageSize = res.obj.size
            this.pageparm.total = res.obj.total
            //
          } else {
            this.$message({
              type: 'error',
              message: res.message,
            })
          }
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },

    //导出
    exportExcel() {
      htmlToExcel.getExcel('#selectTable', '导出的自定义标题')
      this.selectWindow=false
    },
    //显示预览弹窗
    exportExcelSelect() {
      if (this.selectData.length < 1) {
        this.$message.error('请选择要导出的内容！')
        return false
      }
      this.selectWindow = true
    },
    //选中数据
    handleSelectionChange(val) {
      this.selectData = val
    },
    // 重置事件
    flush() {
      this.formInline.name = ''
      this.formInline.lin_status = '渠道状态'
      this.formInline.lin_type = '渠道类型'
      this.formInline.status = '渠道状态'
      this.formInline.type = '渠道类型'
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.id = row.id
        this.editForm.name = row.name
        this.editForm.status = row.status
        this.editForm.num = row.num
        this.editForm.type = row.type
        this.editForm.description = row.description
        this.editForm.addOr = 'edit'
      } else {
        this.title = '添加'
        this.editForm.id = '0'
        this.editForm.name = ''
        this.editForm.status = '渠道状态'
        this.editForm.num = ''
        this.editForm.type = '渠道类型'
        this.editForm.description = ''
        this.editForm.addOr = 'add'
      }
    },

    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          if (this.editForm.addOr == 'add') {
            if (this.editForm.status == '启用') {
              this.editForm.status = 1
            } else {
              this.editForm.status = 0
            }
            this.$axios
              .post('/recruitment/addChannel', this.editForm)
              .then((res) => {
                if (res.code == 200) {
                  this.getdata(this.formInline)
                  this.editFormVisible = false
                } else {
                  this.$message({
                    type: 'info',
                    message: res.message,
                  })
                  this.editFormVisible = false
                }
              })
              .catch()
          }
          if (this.editForm.addOr == 'edit') {
            if (this.editForm.status == '启用') {
              this.editForm.status = 1
            } else {
              this.editForm.status = 0
            }

            this.$axios
              .put('/recruitment/updateChannel/', this.editForm)
              .then((res) => {
                if (res.code == 200) {
                  //修改成功后在刷新页面
                  this.getdata(this.formInline)
                  //关闭修改框
                  this.editFormVisible = false
                } else {
                  this.$message({
                    type: 'info',
                    message: res.message,
                  })
                  //关闭修改框
                  this.editFormVisible = false
                }
              })
              .catch()
          }
        } else {
          return false
        }
      })
    },
    // 删除公司
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          console.log('删除请求发送成功')
          this.$axios
            .delete('/recruitment/deletChannel/' + row.id)
            .then((res) => {
              if (res.code == 200) {
                //重新渲染列表
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'success',
                  message: res.message,
                })
              }
            })
            .catch((err) => {
              this.loading = false
              this.$message.error('用户删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    },
  },
}
</script>

<style scoped lang="less">
.se_input {
  position: relative;
  left: 0px;
  top: 0px;
  width: 190px;
  height: 30px;
  font-family: 'Arial Normal', 'Arial';
  font-weight: 400;
  font-style: normal;
  font-size: 14px;
  text-decoration: none;
  color: #000000;
}
#status_input {
  position: relative;
  left: 20px;
}
</style>